package com.qf.application.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

@Configuration
public class RedisConfig {

    @Bean("JsonRedisTemplate")
    public RedisTemplate<String, Serializable> redisTemplate(@Autowired LettuceConnectionFactory connectionFactory){
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();

        GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer();

        redisTemplate.setKeySerializer(RedisSerializer.string());

        redisTemplate.setValueSerializer(serializer);

        redisTemplate.setHashKeySerializer(serializer);

        redisTemplate.setHashValueSerializer(serializer);

        redisTemplate.setConnectionFactory(connectionFactory);

        return redisTemplate;

    }

}
